package com.zh.business.base.config;

import com.zh.business.modules.sys.domain.*;
import com.zh.business.modules.sys.mapper.SysAuthMapper;
import com.zh.business.modules.sys.mapper.SysRoleAuthMapper;
import com.zh.business.modules.sys.mapper.SysRoleMapper;
import com.zh.business.modules.sys.mapper.SysUserRoleMapper;
import com.zh.business.modules.sys.service.SysUserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @create 2019-12-10 22:46
 * @description
 **/
public class ShiroRealm extends AuthorizingRealm {

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysRoleAuthMapper roleAuthMapper;

    @Autowired
    private SysAuthMapper authMapper;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取登入用户名
        String userName = (String) principalCollection.getPrimaryPrincipal();
        SysUser user = userService.getUserByUserName(userName);
        //添加角色权限
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        SysUserRole ur = new SysUserRole();
        ur.setUserId(user.getId());
        List<SysUserRole> sysUserRoles = userRoleMapper.select(ur);
        if (sysUserRoles != null && sysUserRoles.size() > 0) {
            for (SysUserRole userRole : sysUserRoles) {
                SysRole role = roleMapper.selectByPrimaryKey(userRole.getRoleId());
                info.addRole(role.getName());
                SysRoleAuth ra = new SysRoleAuth();
                ra.setRoleId(role.getId());
                List<SysRoleAuth> sysRoleAuths = roleAuthMapper.select(ra);
                if (sysRoleAuths != null && sysRoleAuths.size() > 0) {
                    for (SysRoleAuth roleAuth : sysRoleAuths) {
                        SysAuth sysAuth = authMapper.selectByPrimaryKey(roleAuth.getAuth());
                        info.addStringPermission(sysAuth.getPermission());
                    }
                }

            }
        }
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String userName = (String) token.getPrincipal();
        SysUser user = userService.getUserByUserName(userName);
        if (null == user)
            return null;
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(
                userName,
                user.getPassword(),
                ByteSource.Util.bytes(user.getSalt()),
                getName());
        return info;
    }
}
